package cn.zs.myfilesyn;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button start_button;
    private Button stop_button;
    private TextView textMessage;
    private TextView file_list;
    private LinearLayout logList;
    private File root;
    private boolean is_run = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    private  void init(){
        start_button = (Button)findViewById(R.id.start_button);
        stop_button = (Button)findViewById(R.id.stop_button);
        textMessage = (TextView)findViewById(R.id.textMessage);
        file_list = (TextView)findViewById(R.id.file_list);
        logList = (LinearLayout)findViewById(R.id.log_list);
        start_button.setOnClickListener(this);
        stop_button.setOnClickListener(this);
    }
    private void start(){
        File ff = Environment.getExternalStorageDirectory();
        if(ff==null){
            Log.d("FragmentActivity1", "complut:ff==null ");

        }else{
            Log.d("FragmentActivity1", "complut:ff= "+ff.getPath());
        }
        File f=Environment.getExternalStorageDirectory();
        Log.d("FragmentActivity1", "absolute path "+    f.getAbsolutePath());
        Log.d("FragmentActivity1", "is dir "+    f.isDirectory());
        Log.d("FragmentActivity1", "canRead  "+    f.canRead());
        Log.d("FragmentActivity1", "canWrite "+    f.canWrite());
        root = new File(ff.getPath()+"/DCIM");
        is_run = true;
        Log.d("FragmentActivity1", "root absolute path "+    root.getAbsolutePath());
        Log.d("FragmentActivity1", "root is dir "+   root.isDirectory());
        Log.d("FragmentActivity1", "root canRead  "+    root.canRead());
        Log.d("FragmentActivity1", "root canWrite "+    root.canWrite());
        complut(root);
    }
    private void complut(File f){
        textMessage.setText(f.getPath());

        if(!is_run){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            textMessage.setText("完成");
            return;
        }
        if(!f.exists()){
            textMessage.setText("不存在");
        }
        if(f.isDirectory()){
            File[] list =  f.listFiles();
            if(list==null){
                Log.d("FragmentActivity1", "complut:list=null ");
            }else
            for(File f0:list){
                complut(f0);
            }
        }else{
            if(f.getName().startsWith("IMG_201")){
                try {
                    Log.d("FragmentActivity1", "complut:du qu wen jian! ");
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    FileInputStream in = new FileInputStream(f);
                    byte[] b = new byte[100];
                    int len = -1;
                    while ((len = in.read(b))>1){
                        out.write(b,0,len);
                    }
                    Log.d("FragmentActivity1", "complut:kai shi ji suan ");
                    MessageDigest digest = MessageDigest.getInstance("SHA-1");
                    digest.update(out.toByteArray());
                    //获取字节数组
                    byte messageDigest[] = digest.digest();
                    // Create Hex String
                    StringBuffer hexString = new StringBuffer();
                    // 字节数组转换为 十六进制 数
                    for (int i = 0; i < messageDigest.length; i++) {
                        String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                        if (shaHex.length() < 2) {
                            hexString.append(0);
                        }
                        hexString.append(shaHex);
                    }
                    Intent intent = new Intent(this, NetListenService.class);
                    startService(intent);
                    String sh1 =  hexString.toString().toUpperCase();
                    Log.d("FragmentActivity1", "complut:sh1= "+sh1);
                    file_list.setText(sh1+":"+f.getName());
                    URL u = new URL("http://192.168.1.116:8080/dome/aa?name="+f.getName()+"&sh1="+sh1);
                    HttpURLConnection con =(HttpURLConnection) u.openConnection();
                    con.connect();
                    InputStream c = con.getInputStream();
                    b = new byte[c.available()];
                    c.read(b);
                    con.disconnect();
                    String b0 = new String(b);
                    Log.d("FragmentActivity1", "complut:server= "+b0);
                    if(b0.equals("OK")){
                        HttpURLConnection con1 =(HttpURLConnection) u.openConnection();
                        con1.setDoOutput(true);
                        con1.connect();
                        OutputStream o1 = con1.getOutputStream();
                        o1.write(out.toByteArray());
                        con1.disconnect();
                        Log.d("FragmentActivity1", "complut:wen jian cheng gong shang chuan  ");
                    }
                }catch (Exception e){
                    Log.d("FragmentActivity1", "error e= "+e.getMessage());
                    e.printStackTrace();
                }

            }
        }

    }
    private void stop(){
        is_run=false;
    }

    @Override
    public void onClick(View v) {
        int  vid = v.getId();
        switch (vid){
            case R.id.start_button :{
                textMessage.setText("开始。。。");
                start();
                break;
            }
            case R.id.stop_button :{
                textMessage.setText("停止。。。");
                break;
            }
        }
    }
}
